package com.zretc.emptraining.daoimpl;

import java.util.List;

import com.zretc.emptraining.dao.CoursePlanDao;
import com.zretc.emptraining.dbtools.DBTools;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.Course;
import com.zretc.emptraining.entity.CoursePlan;

/**
 * 课程计划DAO实现类
 * 
 * @author 傅洁敏
 *
 */
public class CoursePlanDaoImpl implements CoursePlanDao {
	/**
	 * 增加课程计划
	 * 
	 * @param cp 课程计划类
	 * @return boolean 是否添加成功
	 */
	@Override
	public boolean addCoursePlan(CoursePlan cp) {
		if (cp == null) {
			return false;
		} else {
			String sql = "insert into courseplan values(null,?,?,?,?,?,?,?,?)";
			int n = DBTools.getUpdate(sql, cp.getCourseNo(), cp.getCourseStart(), cp.getCourseEnd(), cp.getTeacherNo(),
					cp.getCourseNum(), cp.getCoursePlace(), cp.getCourseTime(),cp.getCourseStatus());
			return n > 0;
		}
	}

	/**
	 * 根据  计划课程编号 删除课程计划
	 * 
	 * @param cpNo  计划课程编号
	 * @return boolean 是否删除成功
	 */
	@Override
	public boolean delCoursePlan(int cpNo) {
		String sql = "delete  from  courseplan  where cpNo=?";
		int n = DBTools.getUpdate(sql, cpNo);
		return n > 0;
	}

	/**
	 * 修改课程计划
	 * 
	 * @param cp 课程计划
	 * @return boolean 是否修改成功
	 */
	@Override
	public boolean updCoursePlan(CoursePlan cp) {
		if (cp == null) {
			return false;
		} else {
			String sql = "update courseplan set courseNo=?,courseStart=?,courseEnd=?,teacherNo=?,courseNum=?,coursePlace=? ,courseTime=?,courseStatus=? where cpNo=?";
			int n = DBTools.getUpdate(sql, cp.getCourseNo(),cp.getCourseStart(), cp.getCourseEnd(),cp.getTeacherNo(), cp.getCourseNum(),
					cp.getCoursePlace(), cp.getCourseTime(),cp.getCourseStatus(),cp.getCpNo());
			return n > 0;
		}
	}

	/**
	 * 分页：根据 教员号 精确查询
	 * 
	 * @param page     页码
	 * @param pagesize 每页显示的记录数
	 * @param keywords 课程名关键字
	 * @return PageData<Course> 课程信息集合
	 */
	@Override
	public PageData<CoursePlan> getCoursePlansByPage(int page, int pageSize, int teacherNo) {
		String sql = "String sql = \"select cpNo,courseplan.courseNo,courseStart,courseEnd,courseplan.teacherNo,courseNum,\"\r\n" + 
				"				+ \"coursePlace,courseTime,courseStatus,courseName,teacherName FROM courseplan LEFT JOIN course \"\r\n" + 
				"				+ \"on courseplan.courseNo=course.courseNo LEFT JOIN teach ON courseplan.teacherNo=teach.teacherNo \"\r\n" + 
				"				+ \"Where teach.teacherNo = ?\";";
		PageData<CoursePlan> pageData = DBTools.exQueryByPage(sql, CoursePlan.class, page, pageSize, teacherNo);
		return pageData;
	}

	/**
	 * 分页：查询所有
	 * 
	 * @param page     页码
	 * @param pagesize 每页显示的记录数
	 * @param keywords 课程名关键字
	 * @return PageData<CoursePlan> 课程信息集合
	 */
	@Override
	public PageData<CoursePlan> getAllCoursePlansByPage(int page, int pageSize) {
		String sql = "select cpNo,courseplan.courseNo,courseStart,courseEnd,courseplan.teacherNo,\r\n" + 
				"courseNum,coursePlace,courseTime,courseStatus,courseName,teacherName \r\n" + 
				"FROM courseplan LEFT JOIN course \r\n" + 
				"on courseplan.courseNo=course.courseNo\r\n" + 
				"LEFT JOIN teach \r\n" + 
				"ON courseplan.teacherNo=teach.teacherNo\r\n";
		PageData<CoursePlan> pageData = DBTools.exQueryByPage(sql, CoursePlan.class, page, pageSize);
		return pageData;
	}
	/**
	 * 根据 课程计划编号 精确查询
	 * @param cpNo  课程计划编号
	 * @return Course 课程类
	 */
	@Override
	public CoursePlan  getCoursePlanByNo(int cpNo) {
		String sql="select cpNo,courseplan.courseNo,courseStart,courseEnd,courseplan.teacherNo,\r\n" + 
				"courseNum,coursePlace,courseTime,courseStatus,courseName,teacherName \r\n" + 
				"FROM courseplan LEFT JOIN course \r\n" + 
				"on courseplan.courseNo=course.courseNo\r\n" + 
				"LEFT JOIN teach \r\n" + 
				"ON courseplan.teacherNo=teach.teacherNo\r\n" +
				"Where cpNo = ?";
		List<CoursePlan> list=(List<CoursePlan>)DBTools.exQuery(sql, CoursePlan.class, cpNo);
		if (list.size()==0) {
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public List<CoursePlan> getCoursePlan() {
		String sql = "select * from courseplan";
		return (List<CoursePlan>) DBTools.exQuery(sql, CoursePlan.class, null);
	}
	
	@Override
	public boolean updCoursePlanStatus(int cpNo, int status) {
		String sql = "update courseplan set courseStatus = ? where cpNo = ?";
		return DBTools.getUpdate(sql, status , cpNo) > 0;
	}

}
